package com.ctshk.rpc.order.train.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.order.train.entity.TrainManualOrder;
import com.ctshk.rpc.order.train.entity.TrainOrder;
import com.ctshk.rpc.order.train.entity.TrainOrderTourist;
import com.ctshk.rpc.order.train.mapper.TrainManualOrderMapper;
import com.ctshk.rpc.order.train.mapper.TrainOrderMapper;
import com.ctshk.rpc.order.train.mapper.TrainOrderTouristMapper;
import com.ctshk.rpc.order.train.req.TrainManualOrderReq;
import com.ctshk.rpc.order.train.req.TrainOrderReq;
import com.ctshk.rpc.order.train.req.TrainOrderTouristReq;
import com.ctshk.rpc.order.train.service.ITrainManualOrderService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 火车票手工单（外挂单） 服务实现类
 * </p>
 *
 * @author 彭柳霖
 * @since 2021-03-17
 */
@DubboService
public class TrainManualOrderServiceImpl extends ServiceImpl<TrainManualOrderMapper, TrainManualOrder> implements ITrainManualOrderService {


    @Autowired
    private TrainOrderMapper trainOrderMapper;

    @Autowired
    private TrainManualOrderMapper trainManualOrderMapper;

    @Autowired
    private TrainOrderTouristMapper trainOrderTouristMapper;

    /**
     * 添加外挂单
     * @param trainManualOrderReq
     * @param user
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result addTrainManualOrder(TrainManualOrderReq trainManualOrderReq, TokenUser user) {

        TrainOrderReq trainOrderReq = trainManualOrderReq.getTrainOrderReq();

        List<TrainOrderTouristReq> touristReqList = trainManualOrderReq.getTrainOrderTouristReqList();


        //添加订单信息
        TrainOrder trainOrder = new TrainOrder();
        BeanUtils.copyProperties(trainOrderReq,trainOrder);
        Long orderId = SnowflakeIdWorker.nextId();
        trainOrder.setId(orderId);
        trainOrder.setCreateId(user.getId());
        trainOrder.setGmtCreate(LocalDateTime.now());
        // 设置外挂单标识
        trainOrder.setOrderType(1);
        trainOrderMapper.insert(trainOrder);

        //添加外挂订单信息
        TrainManualOrder trainManualOrder = new TrainManualOrder();
        BeanUtils.copyProperties(trainManualOrderReq,trainManualOrder);
        trainManualOrder.setId(SnowflakeIdWorker.nextId());
        trainManualOrder.setOrderId(orderId);
        trainManualOrderMapper.insert(trainManualOrder);

        //添加订单旅客信息
        for(TrainOrderTouristReq trainOrderTouristReq : touristReqList){
            TrainOrderTourist trainOrderTourist = new TrainOrderTourist();
            BeanUtils.copyProperties(trainOrderTouristReq,trainOrderTourist);
            trainOrderTourist.setId(SnowflakeIdWorker.nextId());
            trainOrderTourist.setOrderId(orderId);
            trainOrderTourist.setCreateId(user.getId());
            trainOrderTourist.setGmtCreate(LocalDateTime.now());
            trainOrderTouristMapper.insert(trainOrderTourist);
        }

        return Result.success();
    }
}
